//
//  GCXJViewController.m
//  Estate
//
//  Created by kevin on 16/9/5.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#define SQL_INSPECTS_PROCESS_SELECT_DB(proid)  [NSString stringWithFormat:@"SELECT COUNT(*),date(CREATE_TIME) as time,substr(CREATE_TIME,1,4) as year,substr(CREATE_TIME,6,2) as month,substr(CREATE_TIME,9,2) as day FROM INSPECT_PROCESS WHERE PRO_ID='%@' ",proid]

#import "GCXJViewController.h"
#import "YJHBViewController.h"
#import "EndLeaderIndexCell.h"

@interface GCXJViewController ()

@end

@implementation GCXJViewController

- (NSMutableArray *)DataArray {
    if (!_DataArray) {
        _DataArray = [[NSMutableArray alloc] init];
    }
    return _DataArray;
}

-(UITableView *)tableView{
    
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-60-50*BIZ)];
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        [_tableView registerNib:[UINib nibWithNibName:@"EndLeaderIndexCell" bundle:nil] forCellReuseIdentifier:@"detail"];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self setData];
}


- (void)setData{
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    
    FMResultSet *result = [db executeQuery:SQL_INSPECTS_PROCESS_SELECT_DB(PROID)];
    
    NSMutableDictionary *dic;
    
    while ([result next]) {
        
        //查询到日期条件
        NSString* time= [result stringForColumn:@"time"];
        
        //查询到 年
        NSString* year= [result stringForColumn:@"year"];
        //查询到月
        NSString *month =[LSUtility deletaeZero:[result stringForColumn:@"month"]] ;
        //查询到日
        NSString *day =[LSUtility deletaeZero:[result stringForColumn:@"day"]] ;
        
        NSString *sqlStr=[NSString stringWithFormat:@"SELECT * from INSPECT_PROCESS where pro_id = '%@'",PROID];
        
        NSLog(@"%@",sqlStr);
        
        FMResultSet *results=[db executeQuery:sqlStr];
        
        NSMutableArray *DetailArr=[NSMutableArray array];
        
        while ([results next]) {
            
            NSString *name = [results stringForColumn:@"INSPECT_NAME"];
            
            NSString *processId=[results stringForColumn:@"ID"];
            
            NSLog(@"%@",name);
            
            NSDictionary *dics = @{@"name":name,@"id":processId};
            
            [DetailArr addObject:dics];
        }
        
        NSString *counts = [result stringForColumn:@"COUNT(*)"];
        
        //标题
        NSString *timeAndCount = [NSString stringWithFormat:@"%@年(%@条)",year,counts];
        
        dic=[[NSMutableDictionary alloc] initWithObjectsAndKeys:DetailArr,DIC_ARARRY,timeAndCount,DIC_TITILESTRING,[NSNumber numberWithInt:0],DIC_EXPANDED,nil];
        //将字典加入数组
        [self.DataArray addObject:dic];
    }
    
    [db close];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.DataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableDictionary *dic=[self.DataArray objectAtIndex:section];
    NSArray *array=[dic objectForKey:DIC_ARARRY];
    //判断是收缩还是展开
    if ([[dic objectForKey:DIC_EXPANDED]intValue]) {
        return array.count;
    }else{
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    long section=indexPath.section;
    long row=indexPath.row;
    //得到改组下信息
    NSMutableDictionary *dic=[self.DataArray objectAtIndex:section];
    NSArray *array=[dic objectForKey:DIC_ARARRY];
    EndLeaderIndexCell *cell=[EndLeaderIndexCell cellWithTableView:tableView];
    cell.leftView.backgroundColor=[UIColor colorWithRed:0.91 green:0.42 blue:0.28 alpha:1.00];
    if(row==1){
        cell.rightImg.hidden=YES;
        cell.imagerightConstraint.constant=-20.*BIZ;
        cell.leftView.backgroundColor=[UIColor colorWithRed:0.14 green:0.71 blue:0.69 alpha:1.00];
    }
    cell.rightImg.image = [UIImage imageNamed:@"zhenggai"];
    cell.title.text=[array[row] objectForKey:@"name"];
    //    cell.rightImg = [UIImageView alloc]initWithImage:[UIImage imageNamed:@""];
    return  cell;
    
}

//设置分组头的视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[UIView alloc]initWithFrame:CGRectMake(0,0, DEF_SCREEN_WIDTH, CELL_HEIGHT-10)];
    hView.backgroundColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    UIButton* eButton = [[UIButton alloc] init];
    eButton.backgroundColor=[UIColor whiteColor];
    //按钮填充整个视图
    eButton.frame = hView.frame;
    
    
    [eButton addTarget:self action:@selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    //把节号保存到按钮tag，以便传递到expandButtonClicked方法
    eButton.tag = section;
    
    //根据是否展开，切换按钮显示图片
    if ([self isExpanded:section])
        [eButton setImage:  [UIImage imageNamed: @"xiala" ]forState:UIControlStateNormal];
    else
        [eButton setImage: [UIImage imageNamed: @"gengduo1" ]forState:UIControlStateNormal];
    
    //设置分组标题
    [eButton setTitle:[[self.DataArray objectAtIndex:section] objectForKey:DIC_TITILESTRING]forState:UIControlStateNormal];
    
    [eButton setTitleColor:[UIColor colorWithRed:0.68 green:0.68 blue:0.68 alpha:1.00] forState:UIControlStateNormal];
    //设置字体样式
    eButton.titleLabel.font=[UIFont systemFontOfSize:16.*BIZ];
    
    //设置button的图片和标题的相对位置
    
    //4个参数是到上边界，左边界，下边界，右边界的距离
    eButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    [eButton setTitleEdgeInsets:UIEdgeInsetsMake(0,5, 0,0)];
    [eButton setImageEdgeInsets:UIEdgeInsetsMake(0,DEF_SCREEN_WIDTH-30, 0,0)];
    
    //添加btn
    [hView addSubview: eButton];
    return hView;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    //判断是收缩还是展开
    NSMutableDictionary *dic=[_DataArray objectAtIndex:section];
    if ([[dic objectForKey:DIC_EXPANDED]intValue]) {
        return 5;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*BIZ;
}

//控制表头分组表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 46*BIZ;
}

#pragma mark-点击表格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSMutableDictionary *dic=[self.DataArray objectAtIndex:indexPath.section];
    NSArray *array=[dic objectForKey:DIC_ARARRY];
    
    NSDictionary *dics=array[indexPath.row];
    
    NSLog(@"%@",dics);
    
    YJHBViewController *yjhbVC = [[YJHBViewController alloc]init];
    
    yjhbVC.processID = dics[@"id"];
    
    [self.navigationController pushViewController:yjhbVC animated:YES];
    

    
}

#pragma mark -- 内部调用

//对指定的节进行“展开/折叠”操作,若原来是折叠的则展开，若原来是展开的则折叠
-(void)collapseOrExpand:(long)section{
    
    NSMutableDictionary *dic=[_DataArray objectAtIndex:section];
    
    int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];
    if (expanded) {
        [dic setValue:[NSNumber numberWithInt:0]forKey:DIC_EXPANDED];
    }else
    {
        [dic setValue:[NSNumber numberWithInt:1]forKey:DIC_EXPANDED];
    }
}

//返回指定节是否是展开的

-(int)isExpanded:(long)section{
    
    NSDictionary *dic=[_DataArray objectAtIndex:section];
    int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];
    return expanded;
    
}

//按钮被点击时触发
-(void)expandButtonClicked:(id)sender{
    UIButton* btn= (UIButton*)sender;
    long section= btn.tag;//取得tag知道点击对应哪个块
    [self collapseOrExpand:section];
    //刷新tableview
    [self.tableView reloadData];
}




@end
